import Vue from 'vue'
import VueRouter from 'vue-router'
import CampusBinhai from '../views/CampusBinhai.vue'
import CampusChashan from '../views/CampusChashan.vue'
import CampusYueqing from '../views/CampusYueqing.vue'
import StudentSystem from '../views/StudentSystem.vue'
import Login from '../views/Login.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', component: CampusBinhai },
  { path: '/CampusChashan', component:  CampusChashan },
  { path: '/CampusYueqing', component:CampusYueqing },
  { path: '/StudentSystem', component: StudentSystem },
  { path: '/Login', component: Login }  
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach(function (to, from, next) {
   if (to.path === '/StudentSystem') {
    // 要访问后台主页，需要判断是否有 user 登录
    const user = localStorage.getItem('user')
    if (user) {
      next();
      localStorage.removeItem('user'); //清除本地数据
    } else {
      // 没有登录，强制跳转到登录页
      next('/Login')
    }
  } else {
    next()
  }
})

export default router
